// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.forms;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.core.warehouse.IWarehouseView;
import org.dengues.reports.DenguesReportsPlugin;
import org.dengues.reports.editor.design.GraphicalPartFactory;
import org.dengues.reports.editor.design.ReportDesignPaletteFactory;
import org.dengues.reports.editor.design.ReportRootEditPart;
import org.dengues.reports.editor.design.ReportsEditorInput;
import org.dengues.reports.editor.design.models.ReportsDiagram;
import org.dengues.ui.editors.AbstractEditorInput;
import org.dengues.ui.editors.GEFEditorUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-13 qiang.zhang $
 * 
 */
public class ReportDesignEditor extends GraphicalEditorWithFlyoutPalette {

    private GraphicalPartFactory editPartFactoy;

    private ReportsDiagram reportsDiagram;

    /**
     * Qiang.Zhang.Adolf@gmail.com ReportDesignEditor constructor comment.
     */
    public ReportDesignEditor() {
        setEditDomain(new DefaultEditDomain(this));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#getPaletteRoot()
     */
    @Override
    protected PaletteRoot getPaletteRoot() {
        return ReportDesignPaletteFactory.getPaletteRoot();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
     */
    @Override
    protected void configureGraphicalViewer() {
        super.configureGraphicalViewer();
        ScrollingGraphicalViewer viewer = (ScrollingGraphicalViewer) getGraphicalViewer();
        ReportRootEditPart root = new ReportRootEditPart();
        viewer.setRootEditPart(root);
        // hook the viewer into the EditDomain create a function
        getEditDomain().addViewer(viewer);
        // acticate the viewer as selection provider for Eclipse
        getSite().setSelectionProvider(viewer);

        // initialize the viewer with input
        viewer.setEditPartFactory(getEditPartFactory());
    }

    /**
     * Gets default edit part factory.
     */
    protected EditPartFactory getEditPartFactory() {
        if (editPartFactoy == null) {
            editPartFactoy = new GraphicalPartFactory();
        }
        return editPartFactoy;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
     */
    @Override
    protected void setInput(IEditorInput input) {
        super.setInput(input);
        if (input instanceof ReportsEditorInput) {
            ReportsEditorInput editorInput = (ReportsEditorInput) input;
            reportsDiagram = editorInput.getReportsDiagram();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#initializeGraphicalViewer()
     */
    @Override
    protected void initializeGraphicalViewer() {
        super.initializeGraphicalViewer();
        getGraphicalViewer().setContents(reportsDiagram);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public void doSave(IProgressMonitor monitor) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#getPalettePreferences()
     */
    @Override
    protected FlyoutPreferences getPalettePreferences() {
        return GEFEditorUtils.createPalettePreferences(DenguesReportsPlugin.getDefault().getPreferenceStore());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ui.parts.GraphicalEditor#dispose()
     */
    @Override
    public void dispose() {
        if (getEditorInput() instanceof AbstractEditorInput) {
            AbstractEditorInput processEditorInput = ((AbstractEditorInput) getEditorInput());
            processEditorInput.getWarehouseNode().setNodeStatus(ENodeStatus.NORMAL);
            refreshWarehouseView();
        }
        super.dispose();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshWarehouseView".
     */
    protected void refreshWarehouseView() {
        IViewPart findView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                .findView(IWarehouseView.VIEW_ID);
        if (findView instanceof IWarehouseView) {
            ((IWarehouseView) findView).refresh(((AbstractEditorInput) getEditorInput()).getWarehouseNode().getParent());
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getReportTitle".
     * 
     * @return
     */
    public String getReportTitle() {
        return DenguesCorePlugin.getDefault().getDenguesTitle(
                ((AbstractEditorInput) getEditorInput()).getFile().getFullPath().toPortableString().substring(1));
    }

}
